package com.eachonline.smart.web.common;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.eachonline.smart.model.SmartUser;
import com.eachonline.smart.web.interceptor.AppSessionContext;
import com.eachonline.util.StringUtil;

public class CommonUtil {

	/**
	 * 判断手机号码是否正确
	 * @param telephone
	 * @return
	 */
	 public static boolean isMobileNO(String telephone){
		  boolean flag=false;
		  
		  Pattern p=Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
		  Matcher m=p.matcher(telephone);
		  flag=m.matches();
		  return flag;
	  }

	 
		/** 获取两个时间的时间查 如1天2小时30分钟 */
		public static String getDatePoor(Date endDate, Date nowDate) {
		 
		    long nd = 1000 * 24 * 60 * 60;
		    long nh = 1000 * 60 * 60;
		    long nm = 1000 * 60;
		    // long ns = 1000;
		    // 获得两个时间的毫秒时间差异
		    long diff = endDate.getTime() - nowDate.getTime();
		    // 计算差多少天
		    long day = diff / nd;
		    // 计算差多少小时
		    long hour = diff % nd / nh;
		    // 计算差多少分钟
		    long min = diff % nd % nh / nm;
		    // 计算差多少秒//输出结果
		    // long sec = diff % nd % nh % nm / ns;
//		    return day + "天" + hour + "小时" + min + "分钟";
		    return min+"";
		}
		
		/**
		 * 6为验证码
		 * @return
		 */
		public static String getCode(){
	        //验证码
	        String vcode = "";
	        for (int i = 0; i < 6; i++) {
	            vcode = vcode + (int)(Math.random() * 9);
	        }
	        return vcode;
	    }
	
		public static  SmartUser getCurrentUser(HttpServletRequest request)
		{
			String sessionId="";
			if(null!=request.getParameterMap().get("js_sessionId"))
				 sessionId=request.getParameterMap().get("js_sessionId")[0];
				
			SmartUser user= null;
			if(StringUtil.isNotEmpty(sessionId))
			{
				AppSessionContext appSessionContext= AppSessionContext.getInstance();  
				HttpSession  session=appSessionContext.getSession(sessionId);
				if(session!=null)
				 user=(SmartUser) session.getAttribute("currentUser");
			}
			
			return user;
		}
		
		
		
		public static  HttpSession getCurrentSession(HttpServletRequest request)
		{
			String sessionId="";
			if(null!=request.getParameterMap().get("js_sessionId"))
				 sessionId=request.getParameterMap().get("js_sessionId")[0];
				
			if(StringUtil.isNotEmpty(sessionId))
			{
				AppSessionContext appSessionContext= AppSessionContext.getInstance();  
				HttpSession  session=appSessionContext.getSession(sessionId);
				if(session!=null)
				return  session;
			}
			return null;
			
		}
		
		
		public static  void updateCurrentUser(HttpServletRequest request,SmartUser user)
		{
			String sessionId="";
			if(null!=request.getParameterMap().get("js_sessionId"))
				 sessionId=request.getParameterMap().get("js_sessionId")[0];
				
			if(StringUtil.isNotEmpty(sessionId))
			{
				AppSessionContext appSessionContext= AppSessionContext.getInstance();  
				HttpSession  session=appSessionContext.getSession(sessionId);
				 session.setAttribute("currentUser", user);
			}
			
		}
}
